import axios from "axios";
import router from './router'
import {getToken, removeToken} from "./utils/auth";
import {requireData} from "./utils/request";
import APIURL from "./utils/ipconfig";
import {Message} from "element-ui";
import store from './store'

axios.defaults = {
    timeout: 50000
};
let test = ''
const API_RESULT = {
    SUCCESS: '000000',
    ERROR: '0600000',
    TOKEN_ERROR: '009010'
}


axios.defaults.baseApiURL = APIURL.Base_API_URL;

// http request 拦截器
axios.interceptors.request.use(
    config => {
        if (getToken()) {
            config.headers.token = getToken();
        } else {
            config.headers.token = ''
        }
        test = config
        return config
    },
    err => {
        return Promise.reject(err);
    }
)

// http response 拦截器
axios.interceptors.response.use(
    response => {
        if (response.status === 200) {
            // let token = response.headers.authorization;
            // if (token) {
            //     //   如果存在token，name出发refreshToken方法，替换本地的token
            //     this.$store.dispatch('refreshToken', token);
            // }
            if (response.data !== undefined && response.data.dec.code === API_RESULT.TOKEN_ERROR) {
                Message.error("登录过期,请重新登录");
                store.dispatch('LogOut').then(() => {
                    router.replace({path: '/login' })
                })
            } else {
            }
        } else {
            Message.error('服务器内部错误')
        }
        return response;
    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 400:
                    Message.error("错误的请求");
                    break;
                case 401:
                    // Message.error("登录过期，请重新登录");
                    removeToken();
                    //   store.state.user.token =''
                    let redirect = router.currentRoute.fullPath;
                    let to = {
                        path: '/login'
                    }
                    if (redirect.indexOf('login') === -1) {
                        to.query = {redirect: redirect}
                    }
                    router.replace(to).then(r => {
                    });
                    break;
                case 404:
                    Message.error("访问的接口不存在");
                    break;
                case 408:
                    Message.error("请求超时");
                    break;
                case 500:
                    Message.error("服务器内部错误，清理联系管理员！");
                    break;
                case 501:
                    Message.error("服务未实现，清理联系管理员！");
                    break;
                case 502:
                    Message.error("网关错误，清理联系管理员！");
                    break;
                case 504:
                    Message.error("网关超时，清理联系管理员！");
                    break;
                case 505:
                    Message.error("HTTP版本不受支持，清理联系管理员！");
                    break;
            }
            return Promise.reject(error.response.data);
        }else{
            try {
                let obj = {
                    status: 201,
                    data: {
                        dec: {
                            code: '004869',
                            msg: '连接错误，请稍后重试',
                        }
                    }
                }
                return Promise.resolve(obj);
            } catch (e) {
                console.log()
            }
        }

    }, complete => {
    }
)
export function apiService(url, params, type) {
    return requireData.call(this, url, params, type, 'API');
}

export default axios;
